﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace SenseUtil
{
    class TabList
    {
        public const string CSTR_ResourcePath = @"\Resources\";
        public const string CSTR_WindowsPath = @"\Windows\";
        public const string CSTR_FileExt = ".xml";

        public static string[] ListAvailableTabs()
        {
            string resourcePath =  Settings.GetApplicationPathOnly() + CSTR_ResourcePath;
            string[] filelist = null;

            //Check path exists
            if (Directory.Exists(resourcePath))
            {
                filelist = Directory.GetFiles(resourcePath, "*_manila.xml");

                if (filelist.Length > 0)
                {
                    for (int i = 0; i < filelist.Length; i++)
                    {
                        //Strip off the path info & ext
                        filelist[i] = filelist[i].Replace(resourcePath, "");
                        filelist[i] = filelist[i].Replace(CSTR_FileExt, "");
                    }
                }
                else
                {
                    MessageBox.Show("Unable to find xml files in " + TabList.CSTR_ResourcePath);
                }
            }
            else
            {
                MessageBox.Show("Unable to find xml files in " + TabList.CSTR_ResourcePath);
            }
            return filelist;
        }

        public static string[] FetchXmlDetails(string name)
        {
            string fullFilename = Settings.GetApplicationPathOnly() + CSTR_ResourcePath + name + CSTR_FileExt;
            string[] thexmlfile = new string[7];

            //check file exists
            if (File.Exists(fullFilename))
            {
                try
                {
                    //Open as xml document for processing
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(fullFilename);
                    // Locate the root node and 
                    // Create a list of the child nodes of "Details"
                    XmlNodeList nodelist = xmldoc.DocumentElement.GetElementsByTagName("Details");//FirstChild;

                    //Populate the details for the tab
                    foreach (XmlNode aNode in nodelist)
                    {
                        XmlNode theNode;
                        switch (aNode.Name)
                        {
                            case "Details":
                                //Get the attributes
                                theNode = aNode.Attributes.GetNamedItem("Name");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.Name] = theNode.Value;
                                }
                                theNode = aNode.Attributes.GetNamedItem("Author");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.Author] = theNode.Value;
                                }
                                theNode = aNode.Attributes.GetNamedItem("Url");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.Url] = theNode.Value;
                                }
                                theNode = aNode.Attributes.GetNamedItem("UpdateUrl");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.UpdateUrl] = theNode.Value;
                                }
                                theNode = aNode.Attributes.GetNamedItem("Type");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.Type] = theNode.Value;
                                }
                                break;
                        }
                    }

                    // Create a list of the child nodes of "Filename"
                    nodelist = xmldoc.DocumentElement.GetElementsByTagName("Filename");

                    //Populate the filename details for the tab
                    foreach (XmlNode aNode in nodelist)
                    {
                        XmlNode theNode;
                        switch (aNode.Name)
                        {
                            case "Filename":
                                theNode = aNode.Attributes.GetNamedItem("ManilaPath");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.Mode9] = theNode.Value;
                                }
                                break;
                        }
                    }

                    // Create a list of the child nodes of "Page"
                    nodelist = xmldoc.DocumentElement.GetElementsByTagName("Page");
                    //Populate the manila details for the tab
                    foreach (XmlNode aNode in nodelist)
                    {
                        XmlNode theNode;
                        switch (aNode.Name)
                        {
                            case "Page":
                                theNode = aNode.Attributes.GetNamedItem("Name");
                                if (theNode != null)
                                {
                                    thexmlfile[SenseUtil.xTabDetails.PageName] = theNode.Value;
                                }
                                break;
                        }
                    }

                }
                catch
                {
                    MessageBox.Show("Unable to process file " + name);
                }
            }

            return thexmlfile;
        }

        public static bool CheckFilePresent(string filename)
        {
            //For now assume all files are windows directory
            bool Present = false;

            if (File.Exists(CSTR_WindowsPath + filename))
            {
                Present = true;
            }
            return Present;
        }

    }


}
